# 输入一段文字，统计文字的字符总数，
# 并分类统计出大写字母、小写、数字、汉字、其他字符数量

from tkinter import *


class Statistical_character():
    def __init__(self):
        self.root = Tk()
        self.root.title("统计字符数量")
        self.root.geometry("700x350+600+400")
        self.root.resizable(0, 0)
        self.setup_UI()
        # 全局变量
        self.total_count = 0
        self.upper_count = 0
        self.lower_count = 0
        self.number_count = 0
        self.character_count = 0
        self.other_count = 0

    def setup_UI(self):
        # 输入字符串框体
        self.root.Text_input = Text(self.root,
                                    width=50,
                                    height=5,
                                    bg="white",
                                    font=("微软雅黑", 14))
        self.root.Text_input.place(x=10, y=10)

        # 统计字符按钮
        self.root.Button_cal = Button(self.root,
                                      text="统计字符",
                                      width=10,
                                      height=5,
                                      command=self.calculate)
        self.root.Button_cal.place(x=580, y=20)

        # 显示label
        self.root.Label_total = Label(self.root,
                                      text="字符总数：",
                                      font=("微软雅黑", 14))
        self.root.Label_total.place(x=10, y=150)
        self.root.Var_total = StringVar()
        self.root.Label_total = Label(self.root,
                                      textvariable=self.root.Var_total,
                                      font=("微软雅黑", 14),
                                      fg="red")
        self.root.Label_total.place(x=100, y=150)

        self.root.Label_upper = Label(self.root,
                                      text="大写字母：",
                                      font=("微软雅黑", 14))
        self.root.Label_upper.place(x=200, y=150)
        self.root.Var_upper = StringVar()
        self.root.Label_upper = Label(self.root,
                                      textvariable=self.root.Var_upper,
                                      font=("微软雅黑", 14),
                                      fg="red")
        self.root.Label_upper.place(x=290, y=150)

        self.root.Label_lower = Label(self.root,
                                      text="小写字母：",
                                      font=("微软雅黑", 14))
        self.root.Label_lower.place(x=400, y=150)
        self.root.Var_lower = StringVar()
        self.root.Label_lower = Label(self.root,
                                      textvariable=self.root.Var_lower,
                                      font=("微软雅黑", 14),
                                      fg="red")
        self.root.Label_lower.place(x=490, y=150)

        self.root.Label_number = Label(self.root,
                                       text="数字字符：",
                                       font=("微软雅黑", 14))
        self.root.Label_number.place(x=10, y=180)
        self.root.Var_number = StringVar()
        self.root.Label_number = Label(self.root,
                                       textvariable=self.root.Var_number,
                                       font=("微软雅黑", 14),
                                       fg="red")
        self.root.Label_number.place(x=100, y=180)

        self.root.Label_character = Label(self.root,
                                          text="汉字字符：",
                                          font=("微软雅黑", 14))
        self.root.Label_character.place(x=200, y=180)
        self.root.Var_character = StringVar()
        self.root.Label_character = Label(self.root,
                                          textvariable=self.root.Var_character,
                                          font=("微软雅黑", 14),
                                          fg="red")
        self.root.Label_character.place(x=290, y=180)

        self.root.Label_other = Label(self.root,
                                      text="其他字符：",
                                      font=("微软雅黑", 14))
        self.root.Label_other.place(x=400, y=180)
        self.root.Var_other = StringVar()
        self.root.Label_other = Label(self.root,
                                      textvariable=self.root.Var_other,
                                      font=("微软雅黑", 14),
                                      fg="red")
        self.root.Label_other.place(x=490, y=180)

    def calculate(self):
        # 清零
        self.total_count = 0
        self.upper_count = 0
        self.lower_count = 0
        self.number_count = 0
        self.character_count = 0
        self.other_count = 0
        # 获取文本
        content = self.root.Text_input.get("0.0", "end")
        # 去除空格、回车
        content = content.replace(" ", "")
        content = content.replace("\n", "")
        content = content.replace("\t", "")
        # 逐个分类
        for each in content:
            self.total_count += 1
            if each.isupper():
                self.upper_count += 1
            elif each.islower():
                self.lower_count += 1
            elif each.isdigit():
                self.number_count += 1
            elif each.isalpha():
                self.character_count += 1
            else:
                self.other_count += 1

        self.root.Var_total.set(self.total_count)
        self.root.Var_upper.set(self.upper_count)
        self.root.Var_lower.set(self.lower_count)
        self.root.Var_character.set(self.character_count)
        self.root.Var_number.set(self.number_count)
        self.root.Var_other.set(self.other_count)

    def show(self):
        self.root.mainloop()


if __name__ == "__main__":
    this_projeck = Statistical_character()
    this_projeck.show()